package com.augurit.common.calendar.web;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.web.struts2.Struts2Utils;

import com.augurit.common.base.action.CrudActionSupport;
import com.augurit.common.base.action.ExtBaseAction;
import com.augurit.common.base.action.ExtCrudActionSupport;
import com.augurit.common.calendar.convert.CalendarTypeConvertor;
import com.augurit.common.calendar.convert.SysCalendarConvertor;
import com.augurit.common.calendar.model.ExtCalendarType;
import com.augurit.common.calendar.model.ExtCalendarTypes;
import com.augurit.common.calendar.model.ExtCalendarEvents;
import com.augurit.common.calendar.service.ISysCalSharedService;
import com.augurit.common.calendar.service.ISysCalendarService;
import com.augurit.common.calendar.web.form.SysCalSharedForm;
import com.augurit.common.calendar.web.form.SysCalendarForm;
import com.augurit.common.syscode.service.ISysCodeService;
import com.augurit.common.syscode.web.form.SysCodeForm;
import com.augurit.common.system.web.org.form.OmOrgForm;
import com.augurit.common.util.ExtUtils;
import com.augurit.common.util.lang.DateUtils;

@SuppressWarnings("serial")
@Results({
	@Result(name = CrudActionSupport.RELOAD, location = "sys-calendar.action", type = "redirect"),
	@Result(name = CrudActionSupport.SUCCESS, location = "/common/calendar/sys-calendar.jsp"),
	@Result(name = SysCalendarAction.CALENDAR_LIST, location = "/common/calendar/calendar-list.jsp")
})
public class SysCalendarAction extends ExtCrudActionSupport<SysCalendarForm> {

	private static final String CALENDAR_LIST = "calendar-list";
	
	@Autowired
	private ISysCalendarService caEventService;
	
	@Autowired
	private ISysCalSharedService sysCalSharedService;
	
	@Autowired
	private ISysCodeService sysCodeService;
	
	private SysCalendarForm form;
	private Long eventId;
	
	Page<SysCalendarForm> page = new Page<SysCalendarForm>(15);
	
	
	public String showCalendarList() throws Exception {
		return CALENDAR_LIST;
	}
	
	public String getCalendarEvents() throws Exception {
		ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
		caEventService.search(page, form);
		this.renderText(Struts2Utils.getJsonString(page, DateUtils.YYYY_MM_DD_HH_MM_SS));
		return null;
	}
	
	@Override
	public String delete() throws Exception {
		caEventService.deleteCascade(eventId);
		this.extRenderSuccess();
		return null;
	}

	/**
	 * 返回日程事件列表的JSON字符串
	 * @param events 日程事件form列表
	 */
	private void extRenderCalendarEvents(List<SysCalendarForm> events){
		ExtCalendarEvents extCalendarEvents = new ExtCalendarEvents();
		extCalendarEvents.setEvts(events);
		this.renderJson(Struts2Utils.getJsonString(extCalendarEvents, DateUtils.YYYY_MM_DD_HH_MM_SS));
	}
	
	@Override
	public String list() throws Exception {
		ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
		List<SysCalendarForm> list = caEventService.getAllEvents(this.getLoginUserId());
		this.extRenderCalendarEvents(list);
		return null;
	}
	
	/**
	 * 返回日程事件类型的JSON字符串
	 * @param events 日程事件form列表
	 */
	private void extRenderCalendarType(List<ExtCalendarType> calendars){
		ExtCalendarTypes extCalendarType = new ExtCalendarTypes();
		extCalendarType.setCalendars(calendars);
		String json = this.getJsonString(extCalendarType);
		this.renderJson(json);
	}
	
	/**
	 * 发送日程事件类型的JSON字符串
	 * @return
	 * @throws Exception
	 */
	public String getEventType()throws Exception {
		//从数据字典中查询事件类型子项列表
		List<SysCodeForm> list = this.getSysCodeItemsWithBuffer("AOS_CALENDAR_TYPE", sysCodeService);
		//转换ext calendar可识别的事件类型
		List<ExtCalendarType> extCalendarTypes = CalendarTypeConvertor.convertToCombos(list);
		this.extRenderCalendarType(extCalendarTypes);
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		if(eventId != null){
			form = caEventService.get(eventId);
		}
		else{
			form = new SysCalendarForm();
		}
	}

	/**
	 * 添加日程事件
	 */
	@Override
	public String save() throws Exception {
		try{
			caEventService.addCalendarEvent(form);
			this.extRenderSuccess();
		}
		catch(Exception e){
			e.printStackTrace();
			this.extRenderFailture();
		}
		
		return null;
	}
	
	public String input() throws Exception{
		String json = this.getJsonString(form);
		this.renderJson(json);
		return null;
	}
	
	public String getOrgChree() throws Exception{
		List<OmOrgForm> orgUserTree = caEventService.getOrgUserTree();
		List result = SysCalendarConvertor.convertToOrgTrees(orgUserTree);
		this.extRenderListJson(result, false);
		return null;
	}
	
	public String getShareUsers() throws Exception{
		List<SysCalSharedForm> list = sysCalSharedService.getSharedUsers(eventId);
		ExtBaseAction.extRenderGridJson(list);
		return null;
	}
	

	public SysCalendarForm getModel() {
		return form;
	}

	public Long getEventId() {
		return eventId;
	}

	public void setEventId(Long eventId) {
		this.eventId = eventId;
	}
	
}
